package com.jinkaimen.kqb.ui.activity.h5toapp;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.zxing.integration.android.IntentIntegrator;
import com.ht.baselibs.core.EventCode;
import com.ht.baselibs.retrofit.NetCallBack;
import com.ht.baselibs.ui.base.BaseActivity;
import com.ht.baselibs.utils.ActivityUtils;
import com.ht.baselibs.utils.Calculator;
import com.ht.baselibs.utils.SharedPreferencesUtils;
import com.ht.baselibs.utils.UIUtils;
import com.jinkaimen.kqb.R;
import com.jinkaimen.kqb.bean.hss.HsyOrderBean;
import com.jinkaimen.kqb.manager.ApiManager;

import retrofit2.Call;
import retrofit2.Response;

public class InitiativeScanQrCodeActivity extends BaseActivity {
    /**
     * 数字
     */
    private Button num0;
    private Button num1;
    private Button num2;
    private Button num3;
    private Button num4;
    private Button num5;
    private Button num6;
    private Button num7;
    private Button num8;
    private Button num9;
    /**
     * 运算符
     */
    private Button plus_btn;
    private Button account_btn;//收款
    /**
     * 其他
     */
    private Button dot_btn;
    private EditText mEditResultText;//显示算式
    private TextView mTextResultText;
    private StringBuffer buffer = new StringBuffer("");
    private StringBuffer tempBuffer = new StringBuffer("");
    private int type;
    private String title;

    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected boolean hasEvent() {
        return true;
    }

    @Override
    protected void initTitleBar() {
        mTitleBarView.setTitleText(title);

    }

    @Override
    protected void initIntentData(Intent callIntent) {
        type = callIntent.getIntExtra("type", -1);
        title = callIntent.getStringExtra("title");
    }


    @Override
    protected int initContentView() {
        return R.layout.activity_initiative_qo_code;
    }

    @Override
    protected void initChildView() {
        /**
         * 数字
         */
        num0 = (Button) findViewById(R.id.num_zero);
        num1 = (Button) findViewById(R.id.num_one);
        num2 = (Button) findViewById(R.id.num_two);
        num3 = (Button) findViewById(R.id.num_three);
        num4 = (Button) findViewById(R.id.num_four);
        num5 = (Button) findViewById(R.id.num_five);
        num6 = (Button) findViewById(R.id.num_six);
        num7 = (Button) findViewById(R.id.num_seven);
        num8 = (Button) findViewById(R.id.num_eight);
        num9 = (Button) findViewById(R.id.num_nine);
        /**
         * 运算符
         */
        plus_btn = (Button) findViewById(R.id.plus_btn);
        account_btn = (Button) findViewById(R.id.acount_btn);


        /**
         * 其他
         */
        dot_btn = (Button) findViewById(R.id.dot_btn);

        /**
         * 结果
         */
        mTextResultText = (TextView) findViewById(R.id.result_text);
        mEditResultText = (EditText) findViewById(R.id.result_text_new);

    }

    @Override
    protected void initChildData() {
        buffer = new StringBuffer("");
        tempBuffer = new StringBuffer("");
    }

    @Override
    protected void setChildViewListener() {
        num0.setOnClickListener(this);
        num1.setOnClickListener(this);
        num2.setOnClickListener(this);
        num3.setOnClickListener(this);
        num4.setOnClickListener(this);
        num5.setOnClickListener(this);
        num6.setOnClickListener(this);
        num7.setOnClickListener(this);
        num8.setOnClickListener(this);
        num9.setOnClickListener(this);
        plus_btn.setOnClickListener(this);
        dot_btn.setOnClickListener(this);
        account_btn.setOnClickListener(this);
        findViewById(R.id.delete_btn).setOnClickListener(this);


    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {  /**
         * 数字
         */
            case R.id.num_zero:
                if (buffer.toString().equals("0")) {
                    return;
                }

                if (tempBuffer.toString().startsWith("0.") && tempBuffer.length() >= 4) {
                    return;
                }

                if (checkNum()) {
                    return;
                }
                tempBuffer.append("0");
                buffer.append("0");
                showInfo();
                break;

            case R.id.num_one:


                if (checkNum()) {
                    return;
                }
                tempBuffer.append("1");
                buffer.append("1");
                showInfo();
                break;
            case R.id.num_two:


                if (checkNum()) {
                    return;
                }
                tempBuffer.append("2");
                buffer.append("2");
                showInfo();
                break;
            case R.id.num_three:

                if (checkNum()) {
                    return;
                }
                tempBuffer.append("3");
                buffer.append("3");
                showInfo();
                break;
            case R.id.num_four:


                if (checkNum()) {
                    return;
                }
                tempBuffer.append("4");
                buffer.append("4");
                showInfo();
                break;
            case R.id.num_five:


                if (checkNum()) {
                    return;
                }
                tempBuffer.append("5");
                buffer.append("5");
                showInfo();
                break;
            case R.id.num_six:

                if (checkNum()) {
                    return;
                }
                tempBuffer.append("6");
                buffer.append("6");
                showInfo();
                break;
            case R.id.num_seven:


                if (checkNum()) {
                    return;
                }
                tempBuffer.append("7");
                buffer.append("7");
                showInfo();
                break;
            case R.id.num_eight:

                if (checkNum()) {
                    return;
                }
                tempBuffer.append("8");
                buffer.append("8");
                showInfo();
                break;
            case R.id.iv_initiative_qo_code_close:
//                popupWindow.dismiss();
                break;
            case R.id.acount_btn:
                String amount = mEditResultText.getText().toString();


                accordCord(amount);
                break;
            case R.id.num_nine:

                if (checkNum()) {
                    return;
                }
                tempBuffer.append("9");
                buffer.append("9");
                mTextResultText.setText(buffer.toString());

                if (buffer.length() > 0 && !String.valueOf(buffer.charAt(buffer.length() - 1)).equals("+")) {
                    mEditResultText.setText("¥ " + Calculator.conversion(buffer.toString()) + "");
                } else {
                    mEditResultText.setText("¥ " + Calculator.conversion(buffer.toString() + "0") + "");

                }

                if (buffer.length() > 0 && Double.valueOf(Calculator.conversion(buffer.toString())) > 999999.99) {
                    account_btn.setEnabled(false);
                    UIUtils.showToast("超过最大收款金额  999999.99元");
                } else {
                    account_btn.setEnabled(true);
                }
                break;
            /**
             * 运算符
             */
            case R.id.plus_btn: {
                if (tempBuffer.length() > 0) {
                    tempBuffer.replace(0, tempBuffer.length(), "");
                }
                if (buffer.length() > 0 && !String.valueOf(buffer.charAt(buffer.length() - 1)).equals("+")
                        && !String.valueOf(buffer.charAt(buffer.length() - 1)).equals(".")) {
                    buffer.append("+");
                    mTextResultText.setText(buffer.toString());
                }

                break;
            }
            /**
             * 其他
             */
            case R.id.dot_btn: {
                if (buffer.length() > 0 && !String.valueOf(buffer.charAt(buffer.length() - 1)).equals(".") && !tempBuffer.toString().contains(".")) {
                    if (tempBuffer.length() == 0) {
                        tempBuffer.append("0.");
                        buffer.append("0.");
                    } else {
                        tempBuffer.append(".");
                        buffer.append(".");

                    }
                    mTextResultText.setText(buffer.toString());
                }
                break;
            }
            case R.id.delete_btn: {
                String strs = buffer.toString();
                if (strs.length() > 0) {

                    buffer.deleteCharAt(buffer.length() - 1);

                    mTextResultText.setText(buffer.toString());
                    if (buffer.length() > 0 && !String.valueOf(buffer.charAt(buffer.length() - 1)).equals("+") && !String.valueOf(buffer.charAt(buffer.length() - 1)).equals(".")) {
                        mEditResultText.setText("¥ " + Calculator.conversion(buffer.toString()) + "");
                    } else {
                        if (buffer.length() == 0) {
                            mEditResultText.setText("¥ " + Calculator.conversion(buffer.toString() + "0.00") + "");
                            return;
                        }
                        mEditResultText.setText("¥ " + Calculator.conversion(buffer.toString() + "0") + "");
                    }
                }
                String temps = tempBuffer.toString();
                if (temps.length() > 0) {
                    tempBuffer.deleteCharAt(tempBuffer.length() - 1);
                }
                if (buffer.length() > 0 && Double.valueOf(Calculator.conversion(buffer.toString())) > 999999.99) {
                    account_btn.setEnabled(false);
                    UIUtils.showToast("超过最大收款金额  999999.99元");
                } else {
                    account_btn.setEnabled(true);
                }
                break;
            }

        }
    }


    private void accordCord(final String amount) {

        final String replace = amount.replace("¥ ", "");
        double dAmount = Double.parseDouble(replace);

        if (dAmount == 0) {

            UIUtils.showToast("请输入收款金额");
            return;
        }
        ApiManager.getApi().createOrderId(
                "HSS001040",
                dAmount + ""
        ).enqueue(new NetCallBack<HsyOrderBean>() {
            @Override
            protected void onSuccess(Call<HsyOrderBean> call, Response<HsyOrderBean> response) {
                HsyOrderBean hsyOrderBean = response.body();
                if (hsyOrderBean != null) {
                    long ordernumber = hsyOrderBean.businessOrderId;
                    String amount = hsyOrderBean.amount;
                    startScanQrcode(amount, ordernumber);
                }

            }

        });
    }

    private void showInfo() {

        if (buffer.length() > 0 && Double.valueOf(Calculator.conversion(buffer.toString())) > 999999.99) {
            account_btn.setEnabled(false);
            UIUtils.showToast("超过最大收款金额  999999.99元");
        } else {
            account_btn.setEnabled(true);
        }
        mTextResultText.setText(buffer.toString());
        mEditResultText.setText("¥ " + Calculator.conversion(buffer.toString()) + "");
    }


    private boolean checkNum() {
        if (tempBuffer.length() > 1 && tempBuffer.toString().contains(".") && !tempBuffer.toString().endsWith(".")) {

            String temp = tempBuffer.toString().split("\\.")[1];

            if (temp.length() >= 2) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public void startScanQrcode(String amount, long ordernumber) {
//        //                假如你要用的是fragment进行界面的跳转
////                IntentIntegrator intentIntegrator = IntentIntegrator.forSupportFragment(ShopFragment.this)
////
//        IntentIntegrator intentIntegrator = new IntentIntegrator(InitiativeScanQrCodeActivity.this);
//        intentIntegrator
//                .setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES)
//                .setPrompt("")
//                .addExtra("money", amount)
//                .addExtra("ordernumber", ordernumber)
//                .setOrientationLocked(false)//扫描方向固定
////                .setCaptureActivity(AccordQrCodeActivity.class) // 设置自定义的activity是CustomActivity
//                // TODO: 2017/8/25
//                .initiateScan(); // 初始化扫描
        if (type != -1) {
            Bundle bundle = new Bundle();
            bundle.putString("amount", amount);
            bundle.putLong("ordernumber", ordernumber);
            bundle.putInt("type", type);
            ActivityUtils.startActivityAndFinish(this, SelectPaymentMethodActivity.class, bundle);
        } else {
            UIUtils.showToast("系统异常，请退出重试");
        }

    }

    @Override
    protected void onEvent(int eventCode, Object data) {
        super.onEvent(eventCode, data);

        if (eventCode == EventCode.EVENT_REFRESH_PAYSUCCESS) {
            tempBuffer.replace(0, tempBuffer.length(), "");
            buffer.replace(0, buffer.length(), "");
            mEditResultText.setText("¥  0.00");
            mTextResultText.setText("");

        }
    }


}
